﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _3Dshower
{
    public partial class Form1 : Form
    {
        MyCubeModel CUBE;
        Graphics picture;
        bool up;
        public Form1()
        {
            InitializeComponent();
            CUBE = PaintACube();
            picture = this.CreateGraphics();
        }
        
        MyCubeModel PaintACube()
        {
            MyCubeModel cube= new MyCubeModel();
            Point[] points = new Point[14];
            points[0] = new Point(1,1,1);
            points[1] = new Point(1,1,-1);
            points[2] = new Point(-1,1,-1);
            points[3] = new Point(-1,1,1);
            points[4] = new Point(1,-1,1);
            points[5] = new Point(1,-1,-1);
            points[6] = new Point(-1,-1,-1);
            points[7] = new Point(-1,-1,1);
            /*for (int i=0;i<8;++i)//不要再这里转化
            {
                points[i].Coor[0] += 2;
                points[i].Coor[1] += 2;
                points[i].Coor[0] *= 100;
                points[i].Coor[1] *= 100;
                points[i].Coor[2] *= 100;
                points[i].ReCal_CoorToAngle();

            }*/
            line[] lines=new line[12];
            lines[0] = new line(points[0],points[1]);
            lines[1] = new line(points[0], points[3]);
            lines[2] = new line(points[0], points[4]);
            lines[3] = new line(points[1], points[2]);
            lines[4] = new line(points[1], points[5]);
            lines[5] = new line(points[2], points[3]);
            lines[6] = new line(points[2], points[6]);
            lines[7] = new line(points[3], points[7]);
            lines[8] = new line(points[4], points[5]);
            lines[9] = new line(points[4], points[7]);
            lines[10] = new line(points[5], points[6]);
            lines[11] = new line(points[6], points[7]);
            plane[] planes = new plane[6];

            planes[0] = new plane();
            planes[1] = new plane();
            planes[2] = new plane();
            planes[3] = new plane();
            planes[4] = new plane();
            planes[5] = new plane();
            
            points[8]=planes[0].setBounder(new line[4]    { lines[0], lines[1], lines[3], lines[5]});
            points[9] = planes[1].setBounder( new line[4] { lines[1], lines[2], lines[7], lines[9] });
            points[10] = planes[2].setBounder(new line[4] { lines[0], lines[2], lines[4], lines[8] });
            points[11] = planes[3].setBounder( new line[4] { lines[3], lines[4], lines[6], lines[10] });
            points[12] = planes[4].setBounder( new line[4] { lines[5], lines[6], lines[11], lines[7] });
            points[13] = planes[5].setBounder(new line[4] { lines[9], lines[8], lines[10], lines[11] });
            planes[0].color = Color.AliceBlue;
            planes[1].color = Color.Bisque;
            planes[2].color = Color.Blue;
            planes[3].color = Color.Brown;
            planes[4].color = Color.CornflowerBlue;
            planes[5].color = Color.Red;

            cube.planes = planes;
            cube.points = points;
            return cube;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (up)
                CUBE.moveVerAngle(0.01);
            else
                CUBE.moveHorAngle(0.01);//变换有BUG
            picture.Clear(Color.Black);
            drawFace[] canDraw = CUBE.getDrawFace();
            PointF temp=new PointF();
            for (int i = 0; i < canDraw[0].Number; ++i)
            {
                Brush brush = new SolidBrush(canDraw[i].ThisColor);
                picture.FillPolygon(brush,canDraw[i].points);

                picture.DrawLine(new Pen(canDraw[i].ThisColor), 
                                temp.ChangeFromPoint(canDraw[i].lines[0].Boundary[0])
                                , temp.ChangeFromPoint(canDraw[i].lines[0].Boundary[1]));
                picture.DrawLine(new Pen(canDraw[i].ThisColor),
                                temp.ChangeFromPoint(canDraw[i].lines[1].Boundary[0])
                                , temp.ChangeFromPoint(canDraw[i].lines[1].Boundary[1]));
                picture.DrawLine(new Pen(canDraw[i].ThisColor),
                                                temp.ChangeFromPoint(canDraw[i].lines[2].Boundary[0])
                                                , temp.ChangeFromPoint(canDraw[i].lines[2].Boundary[1]));
                picture.DrawLine(new Pen(canDraw[i].ThisColor),
                                                temp.ChangeFromPoint(canDraw[i].lines[3].Boundary[0])
                                                , temp.ChangeFromPoint(canDraw[i].lines[3].Boundary[1]));

            }
        }
        PointF change(PointF p)
        {
            p.X /= 100;
            p.Y /= 100;
            p.X -= 5;
            p.Y -= 5;
            return p;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "W")
                up = true;
            else
                up = false;
        }
    
    }
}
